/*
 * author: wangx
 */
 $(document).ready(function(){
   removeNav();
 });
var allSummary = [];
/* 获取申请列表 */
requestApplyLabList();

function requestApplyLabList() {
  var el = {};
  // el.username = localStorage.session;
  var reqUrl = hostUrl + '/manager/getapply';
  requestWithSession(el, reqUrl, function(data) {
      var box = document.getElementsByClassName('applyList')[0];
      var str = '';
      var list = data.data;
      for (var i = 0; i < list.length; i++) {
        str += "<span class='applyUserName'>" + list[i].username +
          "</span><span class='agreeApply'><button value='" + list[i].username +
          "' class='applyBtn' onclick='agreeApplyBtn(this.value)'>同意</button></span><span class='refuseApply'><button value='" +
          list[i].username + "' class='applyBtn' onclick='refuseApplyBtn(this.value)'>拒绝</button></span>";
      }
      box.innerHTML = str;
    },
    function(error) {
      console.log(error);
    });
}

function agreeApplyBtn(value) {
  showDisGroup(value);
}

function refuseApplyBtn(value) {
  var el = {};
  el.username = value;
  el.level = 0;
  var reqUrl = hostUrl + '/manager/check';
  requestWithSession(el, reqUrl, function(data) {
    if (data.status === 0) {
      alert('拒绝成功!');
      requestApplyLabList();
    }
  }, function(error) {
    console.log(error);
  });
}

/* 显示 分组框*/
function showDisGroup(value) {
  document.getElementsByClassName('applyUser')[0].innerHTML = value;
  document.getElementsByClassName('disGroupBox')[0].style.display = 'block';
}
requestGroupList();

function requestGroupList() {
  // 请求分组列表
  var el = {};
  requestWithSession(el, hostUrl + '/group/get', function(data) {
      var selList = document.getElementById('selectDisGroup');
      var selPerson = document.getElementById('selectPerson');
      var groupList = data.data;
      var str = '';
      var str2 = "<option value='-1'>全部</option>";
      if (groupList.length == 0) {
        str = "<option>没有小组可选</option>";
      }
      for (var i = 0; i < groupList.length; i++) {
        str += "<option value='" + groupList[i].groupid + "'>" + groupList[i].groupname + "</option>";
        str2 += "<option value='" + groupList[i].groupid + "'>" + groupList[i].groupname + "</option>";
      }
      selList.innerHTML = str;
      selPerson.innerHTML = str2;
    },
    function(error) {
      console.log(error);
    });
}
/* 确认分组 */
function disGroupDetermine() {
  var el = {};
  username = document.getElementsByClassName('applyUser')[0].innerHTML;
  el.username = username;
  el.level = 1;
  // 同意加入实验室
  var reqUrl = hostUrl + '/manager/check';
  requestWithSession(el, reqUrl, function(data) {
    if (data.status === 0) {
      requestApplyLabList();
    }
  }, function(error) {
    console.log(error);
  });
  // 请求分组
  reqUrl = hostUrl + '/manager/devide';
  var m = {};
  m.username = username;
  var lselect = document.getElementById('selectDisGroup');
  var index = lselect.selectedIndex;
  var groupid = lselect.options[index].value;
  m.groupid = groupid;
  requestWithSession(m, reqUrl, function(data) {
      if (data.status == 0) {
        alert('分配成功!');
        disGroupCancel();
        requestApplyLabList();
      } else {
        alert('分配失败!');
        disGroupCancel();
      }
    },
    function(error) {
      console.log(error);
    });
}

/* 取消分组 */
function disGroupCancel() {
  document.getElementsByClassName('disGroupBox')[0].style.display = 'none';
}

/* 获取实验室人员列表 */
requestLabUserList(-1);

function requestLabUserList(type) {
  var el = {};
  el.groupid = type;
  requestWithSession(el, hostUrl + '/manager/get', function(data) {
      var list = data.data;
      var box = document.getElementsByClassName('personInfoItemBox')[0];
      var str = '';
      for (var i = 0; i < list.length; i++) {
        str += "<div class='personInfoItem' id='" + list[i].username +
          "' onclick='viewUserInfo(this)'><span class='pITUserName'>" + list[i].username +
          "</span><span class='pITRealName'>" + list[i].realname +
          "</span><span class='pITSexs'>" + list[i].sex +
          "</span><span class='pITAcademy'>" + list[i].academy +
          "</span><span class='pITGrade'>" + list[i].grade +
          "</span><span class='pITClass'>" + list[i].classname +
          "</span><span class='pITTelNum'>" + list[i].tel + "</span></div>";
      }
      box.innerHTML = str;
    },
    function(error) {
      console.log(error);
    })
}

/* 弹出查看成员信息 */
function viewUserInfo(e) {
  console.log(e.id);
  var el = {};
  var realname = '';
  var groupname = '';
  el.username = e.id;
  requestWithSession(el, hostUrl + '/account/getuserinfo', function(data) {
    realname = data.data.realname;
    groupname = data.data.groupid;
  }, function(error) {
    console.log(error);
  }, true);
  // 请求分组
  requestWithSession({}, hostUrl + '/group/get', function(data) {
      for (var i = 0; i < data.data.length; i++) {
        if (groupname == data.data[i].groupid) {
          groupname = data.data[i].groupname;
          break;
        }
      }
    },
    function(error) {
      console.log(error);
    }, true);
  var namebox = document.getElementById('pVUserRealName');
  namebox.innerText = realname;
  var groupbox = document.getElementById('pVUser');
  groupbox.innerText = groupname;
  // 获取他的近期安排
  var el = {};
  el.username = e.id;
  requestWithSession(el, hostUrl + '/record/getrecent', function(data) {
    var list = data;
    var box =document.getElementsByClassName('pVURecentItemBox')[0];
    var str = '';
    for(var i = 0;i<list.length;i++){
      str += "<div class='pVURecentItem'><span class='pVURecentPlanLevel'>"+
      list[i].level + "</span><span class='pVURecentPlanTime'>" +
      list[i].startdate + "-" + list[i].enddate +
      "</span><span class='pVURecentPlanContent'>" +
      list[i].content+"</span></div>";
    }
    box.innerHTML = str;
    },
    function(error) {
      console.log(error);
    },true);
  /* 请求周总结 */
  var el = {};
  el.username = e.id;
  requestWithSession(el,hostUrl + '/record/getsummary',function(data){
    var termStr = ["2016秋", "2017春", "2017秋", "2017春", "2018秋", "2019春"];
    var box = document.getElementsByClassName('pVUWeekSummaryItemBox')[0];
    var str = '';
    allSummary = data.data;
    for(var i=0;i<allSummary.length;i++){
      str += "<div class='pVUWeekSummaryItem'><span class='pVUWeekSummaryTerm'>"+
      termStr[allSummary[i].term - 1] + "</span><span class='pVUWeekSummaryWeek'>第" +
      allSummary[i].week + "周</span><span class='pVUWeekSummaryTitleText'>" +
      allSummary[i].title + "</span><span class='pVUWeekSummaryViewText'><button value='"+
      allSummary[i].id + "' onclick='viewThisUserWeekSummary(this.value)'>查看</button></span></div>";
    }
    box.innerHTML = str;
  },function(error){
    console.log(error);
  },true);
  document.getElementsByClassName('personViewUserInfo')[0].style.display = 'block';
}

/* 关闭 查看个人信息*/
function closeViewUserInfo() {
  document.getElementsByClassName('personViewUserInfo')[0].style.display = 'none';
}

function viewThisUserWeekSummary(id){
  var box = document.getElementsByClassName('viewUserWeekSummaryContent')[0];
  var showSumStr = '';
  for (var i = 0; i < allSummary.length; i++) {
    if (allSummary[i].id == id) {
      showSumStr += "<h4>" + allSummary[i].title + "</h4><p>" + allSummary[i].content + "</p>";
    }
  }
  box.innerHTML = showSumStr;
  document.getElementsByClassName('viewUserWeekSummaryBox')[0].style.display = 'block';
}

function closeViewSummary(){
  document.getElementsByClassName('viewUserWeekSummaryBox')[0].style.display = 'none';
}
